﻿using System.Collections.Generic;

namespace GenericTest
{
	public class Employee
	{
		private string _name;
		private int _id;

		public Employee(string s, int i)
		{
			_name = s;
			_id = i;
		}

		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public int Id
		{
			get { return _id; }
			set { _id = value; }
		}
	}

	public class GenericList<T> where T: Employee
	{
		private class  Node
		{
			private Node _next;
			private T _data;

			public Node(T t)
			{
				this._data = t;
				this._next = null;
			}

			public Node Next
			{
				get { return _next; }
				set { this._next = value; }
			}

			public T Data
			{
				get { return _data; }
				set { this._data = value; }
			}
		}

		private Node _head;

		public GenericList()
		{
			_head = null;
		}

		public void AddHead(T t)
		{
			Node n = new Node(t);
			n.Next = _head;
			_head = n;
		}

		public IEnumerable<T> GetEnumerator()
		{
			Node current = _head;

			while (current != null)
			{
				yield return current.Data;
				current = current.Next;
			}
		}

		public T FindFirstOccurrence(string s)
		{
			Node current = _head;
			T t = null;

			while (current != null)
			{
				//The constraint enables access to the Name property.
				if (current.Data.Name == s)
				{
					t = current.Data;
					break;
				}
				else
				{
					current = current.Next;
				}
			}
			return t;
		}
	}
}